//
//  CRChelpCenterController.m
//  jinyishe
//
//  Created by 金艺社 on 2018/2/24.
//  Copyright © 2018年 wy. All rights reserved.
//

#import "CRChelpCenterController.h"
#import "NetworkManager.h"
#import "CRChelpCenterUrl.h"
#import "CRChelpInfoModel.h"
#import <MJExtension/MJExtension.h>
#import "CRChelpCenterProblemControllerViewController.h"
@interface CRChelpCenterController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainView;
@property (nonatomic, strong) NSMutableArray *infoData;
@end

@implementation CRChelpCenterController

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self.view addSubview:self.mainView];
        [self createConstraints];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"帮助中心";
    [self handleUserInfoDataRequest];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - constraint
- (void)createConstraints {
    self.mainView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - data
- (void)handleUserInfoDataRequest
{
    __weak typeof(self) weakSelf = self;
    [[NetworkManager defaultManager] check_getWithURLString:[CRChelpCenterUrl helpInfo] parameters:nil success:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:kNetResponse_R0001]) {
            NSArray *articleCategoryList = responseObject[@"articleCategoryList"];
            for (NSDictionary *dic in articleCategoryList) {
                CRChelpInfoModel *model = [CRChelpInfoModel mj_objectWithKeyValues:dic];
                [weakSelf.infoData addObject:model];
            }
            [weakSelf.mainView reloadData];
        }

    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _infoData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CELL = @"helpCenterCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    CRChelpInfoModel *model = [_infoData objectAtIndex:indexPath.row];
    cell.textLabel.text = model.name;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CRChelpCenterProblemControllerViewController *VC = [CRChelpCenterProblemControllerViewController new];
    VC.touchSection = [NSString stringWithFormat:@"%ld", indexPath.row];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - lazy Loading
- (UITableView *)mainView {
    if (!_mainView) {
        _mainView = [UITableView new];
        _mainView.dataSource = self;
        _mainView.delegate = self;
        _mainView.tableFooterView = [UIView new];
    }
    return _mainView;
}
- (NSMutableArray *)infoData {
    if (!_infoData) {
        _infoData = [NSMutableArray array];
    }
    return _infoData;
}
@end
